package com.dg.cuckoo.admin.service.impl;

import com.dg.cuckoo.admin.dto.proCarryOut.*;
import com.dg.cuckoo.admin.repository.*;
import com.dg.cuckoo.admin.service.ProjectInfoService;
import com.dg.cuckoo.admin.service.ProjectMoneyService;
import com.dg.cuckoo.admin.vo.ProjectMoneyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class ProjectMoneyServiceimpl implements ProjectMoneyService {

    @Autowired
    private ProjectInfoService projectInfoServer;
    @Autowired
    private BasicProjectRepository basicProjectRepository;
    @Autowired
    private ProCarryOutRepository proCarryOutRepository;
    @Autowired
    private ProChangeRepository proChangeRepository;
    @Autowired
    private AppropriationRepository appropriationRepository;
    @Autowired
    private ProExpendRepository proExpendRepository;


    @Override
    public List<ProjectMoneyVo> query(List<String> oneLabel, List<String> twoLabel, String unit, String year, String mainName) {
        List<String> ids = projectInfoServer.findIdsByList(oneLabel, twoLabel, unit, year, mainName);
        System.err.println("ids:"+ids);
        List<ProjectMoneyVo> data=new ArrayList<>();
        for (String id:ids){
            ProjectMoneyVo vo = new ProjectMoneyVo();
            vo.setProjectId(id);
            vo.setProjectName(basicProjectRepository.findById(id).get().getMainName());
            String htsg = proCarryOutAdd(proCarryOutRepository.findByProjectIdAndType(id, 4));//施工
            vo.setHsg(htsg);
            String htqt = htqtSum(id);
            vo.setHqt(htqt);
            vo.setHxj(new BigDecimal(htsg).add(new BigDecimal(htqt)).toString());

            String gby = proChangeAdd(proChangeRepository.findByProjectIdAndChangeType(id, 1));
            vo.setGby(gby);
            String gbes = new BigDecimal(proChangeAdd(proChangeRepository.findByProjectIdAndChangeType(id, 2))).add(new BigDecimal(proChangeAdd(proChangeRepository.findByProjectIdAndChangeType(id, 3)))).toString();
            vo.setGbes(gbes);
            vo.setGxj(new BigDecimal(gby).add(new BigDecimal(gbes)).toString());

            String bfsj = appropriationAdd(appropriationRepository.findByProjectIdAndType(id, 1));
            String bfbj = appropriationAdd(appropriationRepository.findByProjectIdAndType(id, 2));
            String bfzc = appropriationAdd(appropriationRepository.findByProjectIdAndType(id, 3));
            String bfxj=new BigDecimal(bfsj).add(new BigDecimal(bfbj)).add(new BigDecimal(bfzc)).toString();

            vo.setBfxj(bfxj);
            vo.setBfsj(bfsj);
            vo.setBfbj(bfbj);
            vo.setBfzc(bfzc);

            String ht = proExpendSum(id);
            vo.setHtzc(ht);
            String fhtzc = proExpendAdd(proExpendRepository.findByProjectIdAndType(id,9));
            vo.setFhtzc(fhtzc);
            vo.setZcxj(new BigDecimal(ht).add(new BigDecimal(fhtzc)).toString());
            data.add(vo);
        }
        return data;
    }



    public String appropriationAdd(List<AppropriationDto> dtos){
        if(dtos.isEmpty()){
            return "0";
        }
        BigDecimal bigDecimal = new BigDecimal("0");
        for(AppropriationDto a:dtos){
            bigDecimal=bigDecimal.add(new BigDecimal(a.getAmount()));
        }
        return bigDecimal.toString();
    }


    public String proExpendAdd(List<ProExpendDto> dtos){
        if(dtos.isEmpty()){
            return "0";
        }
        BigDecimal bigDecimal = new BigDecimal("0");
        for(ProExpendDto a:dtos){
            bigDecimal=bigDecimal.add(new BigDecimal(a.getAmount()));
        }
        return bigDecimal.toString();
    }

    public String proExpendSum(String id){
        List<ProExpendDto> dtos=new ArrayList<>();
        dtos.addAll(proExpendRepository.findByProjectIdAndType(id,1));
        dtos.addAll(proExpendRepository.findByProjectIdAndType(id,2));
        dtos.addAll(proExpendRepository.findByProjectIdAndType(id,3));
        dtos.addAll(proExpendRepository.findByProjectIdAndType(id,4));
        dtos.addAll(proExpendRepository.findByProjectIdAndType(id,5));
        dtos.addAll(proExpendRepository.findByProjectIdAndType(id,6));
        dtos.addAll(proExpendRepository.findByProjectIdAndType(id,7));
        dtos.addAll(proExpendRepository.findByProjectIdAndType(id,8));
        return proExpendAdd(dtos);
    }

    public String proChangeAdd(List<ProChangeDto> dtos){
        if(dtos.isEmpty()){
            return "0";
        }
        BigDecimal bigDecimal = new BigDecimal("0");
        for(ProChangeDto a:dtos){
            bigDecimal=bigDecimal.add(new BigDecimal(a.getAmount()));
        }
        return bigDecimal.toString();
    }

    public String proCarryOutAdd(List<ProCarryOutDto> dtos){
        if(dtos.isEmpty()){
            return "0";
        }
        BigDecimal bigDecimal = new BigDecimal("0");
        for(ProCarryOutDto a:dtos){
            bigDecimal=bigDecimal.add(new BigDecimal(a.getAmount()));
        }
        return bigDecimal.toString();
    }

    public String htqtSum(String id){
        List<ProCarryOutDto> dtos =new ArrayList<>();
        dtos.addAll(proCarryOutRepository.findByProjectIdAndType(id, 1));
        dtos.addAll(proCarryOutRepository.findByProjectIdAndType(id, 2));
        dtos.addAll(proCarryOutRepository.findByProjectIdAndType(id, 3));
        dtos.addAll(proCarryOutRepository.findByProjectIdAndType(id, 5));
        dtos.addAll(proCarryOutRepository.findByProjectIdAndType(id, 6));
        dtos.addAll(proCarryOutRepository.findByProjectIdAndType(id, 7));
        dtos.addAll(proCarryOutRepository.findByProjectIdAndType(id, 8));
        return proCarryOutAdd(dtos);
    }
}
